<template>
  <div class="xtx-city" ref="target">
    <div class="select"
    @click="toggleDialog"
    :class="{active:visible}"
    >
      <span class="placeholder" v-if="!fullLocation">请选择配送地址</span>
      <span class="value" v-else >{{fullLocation}}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option"
    v-show="visible"
    >
    <div v-if="loading" class="loading"></div>
     <template v-else>
       <span class="ellipsis" @click="changeItem(item)" v-for="item in curList" :key="item.code">{{item.name}}</span></template>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
import { computed, reactive, ref } from 'vue'
import { onClickOutside } from '@vueuse/core'
export default {
  name: 'XtxCity',
  props: {
    fullLocation: {
      type: String,
      default: ''
    }
  },
  setup (props, { emit }) {
    const loading = ref(false)
    // 控制展开收起，默认收起
    const visible = ref(false)
    const openDialog = () => {
      visible.value = true

      if (window.cityData) {
        console.log('有上传保存的数据')
        cityData.value = window.cityData
      } else {
        console.log('没有上传保存的数据')
        loading.value = true // 发请求前
      }
      // ajax
      getCityData().then(res => {
        cityData.value = res.data
        window.cityData = res.data
        loading.value = false
      })
    }
    const closeDialog = () => {
      visible.value = false
      // 清空选择结果
      for (const key in selectResult) {
        selectResult[key] = ''
      }
    }
    // 切换展开收起
    const toggleDialog = () => {
      if (visible.value) closeDialog()
      else openDialog()
    }
    const target = ref(null)
    onClickOutside(target, () => {
      closeDialog()
    })
    const cityData = ref([])
    const url = 'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json'
    // 获取省市区数据
    const getCityData = () => {
      return axios({ url })
    }
    const selectResult = reactive({
      provinceCode: '', // 省的code
      provinceName: '', // 省的名字
      cityCode: '', // 市的code
      cityName: '', // 市的名字
      countyCode: '', // 区的code
      countyName: '', // 区的名字
      fullLocation: ''// 组合
    })
    // 当前显示的地区的内容，由用户的选择的变化来定的
    // 这里用户计算属性
    const curList = computed(() => {
      let province = {}
      let result = cityData.value
      if (selectResult.provinceCode) {
      // 当前用户选择了省，返回对应的市级信息
        province = cityData.value.find(it => it.code === selectResult.provinceCode)
        result = province.areaList
      }
      if (selectResult.cityCode) {
        const city = province.areaList.find(it => it.code === selectResult.cityCode)
        result = city.areaList
      }
      return result
    })
    const changeItem = (item) => {
    // 点击省份保存用户的选择
      if (item.level === 0) {
        selectResult.provinceCode = item.code
        selectResult.provinceName = item.name
      }
      // 点击市保存用户的选择
      if (item.level === 1) {
        selectResult.cityCode = item.code
        selectResult.cityName = item.name
      }
      if (item.level === 2) {
        selectResult.countyCode = item.code
        selectResult.countyName = item.name
        // 通知父组件用的选择
        selectResult.fullLocation = selectResult.provinceName + selectResult.cityName + selectResult.countyName
        emit('address-change', selectResult)
        console.log(selectResult.fullLocation, '00000000000')
        // 关闭弹层
        closeDialog()
      }
    }
    return { changeItem, curList, visible, toggleDialog, target, getCityData, cityData, loading }
  }
}
</script>
<style scoped lang="less">
.loading {
      height: 290px;
      width: 100%;
      background: url('~@/assets/images/loading.gif') no-repeat center;
    }
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
